<?php
namespace Modules\Goods\Http\Controllers\Admin;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Layout\Content;
use Modules\Goods\Entities\Goods;
use App\Http\Controllers\Controller;
use Modules\Goods\Entities\Category;
use Encore\Admin\Controllers\HasResourceActions;
use Modules\Goods\Http\Controllers\Admin\CategoryService;

class GoodsController extends Controller
{
    use HasResourceActions;

    public function __construct(CategoryService $sCategory)
    {
        $this->sCategory = $sCategory;
    }

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('商品管理')
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Goods);

//        // 禁用导出数据按钮
//        $grid->disableExport();

        // 设置过滤器表单
        $grid->filter(function($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            // 字段过滤器
            $filter->equal('category_id')->select(Category::selectOptions($this->sCategory->selectOptions()));
            $filter->equal('sn');

        });

        $grid->id('ID')->sortable();

        $grid->thumb('微缩图片')->image(40, 40);
        $grid->name('名称');
        $grid->sn('条形码');
        $grid->price('价格')->editable();
        // $grid->market_price('市场售价');
        $grid->specification('规格');
        $grid->brand('品牌');

        $grid->category('分类')->display(function ($item) {
            return $item['id'] . '.'. $item['title'];
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Goods::findOrFail($id));

        $show->id('Id');
        $show->name('Name');
        $show->sn('Sn');
        $show->category('Category');
        $show->category_id('Category id');
        $show->price('Price');
        $show->market_price('Market price');
        $show->specification('Specification');
        $show->brand('Brand');
        $show->weight('Weight');
        $show->keywords('Keywords');
        $show->brief('Brief');
        $show->desc('Desc');
        $show->thumb('Thumb');
        $show->image('Image');
        $show->type('Type');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Goods);

        $form->text('name', '名称');
        $form->text('sn', '条形码');
        // $form->text('category', 'Category');
        $form->select('category_id', '分类')->options(Category::selectOptions($this->sCategory->selectOptions()));
        $form->number('price', 'Price');
        // $form->number('market_price', 'Market price');
        $form->text('specification', 'Specification');
        $form->text('brand', 'Brand');
        // $form->number('weight', 'Weight');
        // $form->text('keywords', 'Keywords');
        // $form->text('brief', 'Brief');
        // $form->textarea('desc', 'Desc');
        $form->image('thumb', 'Thumb');
        // $form->image('image', 'Image');
        // $form->number('type', 'Type');

        return $form;
    }
}
